package lpca.utils 
{
	/**
	 * ...
	 * @author Figishe
	 */
	public class Matrix 
	{
		private var xRow: Vector.<Vector.<int>>;
		private var yRow: Vector.<int>;
		
		private var _sizeX: int;
		private var _sizeY: int;
		
		public function Matrix(_sizeX: int = 1, _sizeY: int = 1, values: Vector.<int> = null) 
		{
			this._sizeX = _sizeX;
			this._sizeY = _sizeY;
			
			xRow = new Vector.<Vector.<int>>(_sizeX);
			
			for (var x: int = 0; x < _sizeX; x++)
			{
				xRow[x] = new Vector.<int>(_sizeY);
				for (var y: int = 0; y < _sizeY; y++)
				{
					if (values != null)
					{
						if (_sizeY * x + y < values.length)
							xRow[x][y] = values[y * _sizeX + x];
						else
							xRow[x][y] = null;
					}
					else
					{
						xRow[x][y] = null;
					}
				}
			}
		}
		
		public function val(x: int, y: int): int
		{
			return xRow[x][y];
		}
		
		public function setVal(x: int, y: int, value: int): void
		{
			xRow[x][y] = value;
		}
		
		public function get sizeX(): int
		{
			return _sizeX;
		}
		
		public function get sizeY(): int
		{
			return _sizeY;
		}
		
		public function mirrorY(): Matrix 
		{
			var newMatrix: Matrix = new Matrix(_sizeX, _sizeY);
			
			for (var y: int = 0; y < _sizeY; y++)
			{
				for (var x: int = 0; x < _sizeX; x++)
				{
					var newVal:int = this.val(x, _sizeY - y - 1);
					newMatrix.setVal(x, y, newVal);
				}
			}
			
			return newMatrix;
		}
		
		public function mirrorX(): Matrix 
		{
			var newMatrix: Matrix = new Matrix(_sizeX, _sizeY);
			
			for (var y: int = 0; y < _sizeY; y++)
			{
				for (var x: int = 0; x < _sizeX; x++)
				{
					var newVal:int = this.val(_sizeX - x - 1, y);
					newMatrix.setVal(x, y, newVal);
				}
			}
			
			return newMatrix;
		}
		
		
		public function rotate90(): Matrix 
		{
			var newMatrix: Matrix = new Matrix(_sizeY, _sizeX);
			
			for (var y: int = 0; y < _sizeY; y++)
			{
				for (var x: int = 0; x < _sizeX; x++)
				{
					var newVal:int = this.val(x, _sizeY - y - 1);
					newMatrix.setVal(y, x, newVal);
				}
			}
			
			return newMatrix;
		}
		
		public function rotate270(): Matrix 
		{
			var newMatrix: Matrix = new Matrix(_sizeY, _sizeX);
			
			for (var y: int = 0; y < _sizeY; y++)
			{
				for (var x: int = 0; x < _sizeX; x++)
				{
					var newVal:int = this.val(_sizeX - x - 1, y);
					newMatrix.setVal(y, x, newVal);
				}
			}
			
			return newMatrix;
		}
		
		public function show(): void
		{
			for (var y: int = 0; y < _sizeY; y++)
			{
				var s: String = '';
				for (var x: int = 0; x < _sizeX; x++)
				{
					s += val(x,y).toString() + ' ';
				}
				trace(s);
			}
		}
		
	}

}